package com.eystar.pdc.controller;

import java.util.List;

import com.eystar.ext.kit.page.PageKit;
import com.eystar.ext.route.ControllerBind;
import com.eystar.pdc.aop.auth.AuthAnno;
import com.eystar.pdc.aop.auth.AuthAnno.AuthType;
import com.eystar.pdc.aop.log.LogAnno;
import com.eystar.pdc.aop.log.LogAnno.LogType;
import com.eystar.pdc.model.Role;
import com.eystar.pdc.model.User;
import com.jfinal.plugin.activerecord.Page;

/**
 * 角色管理
 * @author Administrator
 *
 */
@ControllerBind(controllerKey="/role", viewPath="/pdc")
@AuthAnno(type = AuthType.Super, res = "role")
public class RoleController extends BaseController {
	
	/**
	 * 列表展示页面
	 * @throws Exception
	 */
	@AuthAnno(auth = "look")
	@LogAnno(desc = "查看角色列表", type = LogType.SEARCH)
	public void index() throws Exception{
		render("role_list.html");
	}
	
	/**
	 * 分页查找
	 * @throws Exception
	 */
	public void page() throws Exception{
		Page<Role> page = Role.dao.paginate(getAttr("PageOrder"), getModel(Role.class));
		renderJson(PageKit.toJSON(page));
	}
	
	/**
	 * 跳转到添加页面
	 * @throws Exception
	 */
	@AuthAnno(auth = "save")
	public void add() throws Exception{
		render("role_form.html");
	}
	
	/**
	 * 点击保存按钮
	 * @throws Exception
	 */
	@AuthAnno(auth = "save")
	@LogAnno(desc = "添加角色${role.role_name}", type = LogType.SAVE)
	public void save() throws Exception{
		Role authSign = getSaveModel(Role.class);
		authSign.save();
		renderJson(ok());
	}
	
	/**
	 * 跳转到修改页面
	 * @throws Exception
	 */
	@AuthAnno(auth = "update")
	public void edit() throws Exception{
		setAttr("role", Role.dao.findById(getPara()));
		render("role_form.html");
	}
	
	/**
	 * 点击修改按钮
	 * @throws Exception
	 */
	@AuthAnno(auth = "update")
	@LogAnno(desc = "修改角色${role.role_name}", type = LogType.UPDATE)
	public void update() throws Exception{
		Role authSign = getModel(Role.class);
		authSign.update();
		renderJson(ok());
	}
	
	/**
	 * 支持多个删除，以英文逗号隔开
	 * @throws Exception
	 */
	@AuthAnno(auth = "delete")
	@LogAnno(desc = "删除角色", type = LogType.DELETE)
	public void delete() throws Exception{
		String ids = getPara("ids");
		List<User> users = User.dao.findUserByRoloIds(ids);
		if (users == null || users.size() == 0) {
			Role.dao.deleteByIds(getPara("ids"));
			renderJson(ok());
		}else {
			renderJson(fail("角色下存在用户，禁止删除！"));
		}
	}
}



